package com.summer.safe.quartz.config;

import com.summer.safe.quartz.utils.QuartzLogUtils;
import com.summer.safe.quartz.entity.QuartzJob;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 定时任务的抽象类
 */
public abstract  class QuartzAbstract implements Job {
    private Logger logger = LoggerFactory.getLogger(getClass());
    /**
     * 定时任务执行
     * @param context
     * @throws JobExecutionException
     */
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        //获得JobDetail存入的参数
        QuartzJob quartzJob = (QuartzJob) context.getMergedJobDataMap().get(QuartzJob.TASK_TARGET);
        QuartzLogUtils.start();
        try {
            runExecute(quartzJob);
            QuartzLogUtils.end(quartzJob,null);
        } catch (Exception e) {
            QuartzLogUtils.end(quartzJob,e);
            logger.error("异常:", e);
            e.printStackTrace();
        }

    }

    /**
     * 执行的抽象方法
     * @param quartzJob
     * @throws Exception
     */
    protected abstract void runExecute( QuartzJob quartzJob) throws Exception;
}
